#include "Logger.h"

inline void logger_info(const char *msg){
    logger_info_fmt("%s", msg);
}

inline void logger_err(const char *msg){
    logger_err_fmt("%s", msg);
}

inline void logger_warn(const char *msg){
    logger_warn_fmt("%s", msg);
}

void logger_info_fmt(const char *fmt, ...){
    va_list args;
    va_start(args, fmt);
    logger_fmt(LOG_TAG, INFO, fmt, args);
    va_end(args);
}

void logger_warn_fmt(const char *fmt, ...){
    va_list args;
    va_start(args, fmt);
    logger_fmt(LOG_TAG, WARN, fmt, args);
    va_end(args);
}

void logger_err_fmt(const char *fmt, ...){
    va_list args;
    va_start(args, fmt);
    logger_fmt(LOG_TAG, ERROR, fmt, args);
    va_end(args);
}

void logger_fmt(const char *tag, int level, const char *fmt, va_list args){
    u_char tmp_fmt[MAX_512_LEN] = { '\0' };
    u_char tmp_level[MAX_16_LEN] = { '\0' };
    switch (level){
        case INFO:
            snprintf((char *)tmp_level, MAX_16_LEN, "[+]");
            break;
        case ERROR:
            snprintf((char *)tmp_level, MAX_16_LEN, "[-]");
            break;
        case WARN:
            snprintf((char *)tmp_level, MAX_16_LEN, "[!]");
            break;
        default:
            snprintf((char *)tmp_level, MAX_16_LEN, "[?]");
            break;
    }
    snprintf((char *)tmp_fmt, MAX_512_LEN, "%s[%s] %s", (char *)tmp_level, tag, fmt);
    vprintf((char *)tmp_fmt, args);
}

void *candy_malloc(const char *objName, int size){
    void *malloc_ret = (void*)malloc(size * sizeof(char)); 
    if (malloc_ret){
        memset(malloc_ret, '\0', size);
    }
    if(debug_logger) printf("[candy_malloc] %20s: %p, size=%d\n", objName, malloc_ret, size);
    return malloc_ret;
}

void candy_free(const char *objName, void *obj){
    if(debug_logger) printf("[candy_free] %20s: %p,\n", objName, obj);
    if(obj) {
        free(obj);
        obj = NULL;
    }
}